package com.kit.data.kitopeso.adapter;

import android.annotation.SuppressLint;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kit.data.kitopeso.KitoApp;
import com.kit.data.kitopeso.R;
import com.kit.data.kitopeso.base.BaseBindingAdapter1;
import com.kit.data.kitopeso.databinding.KitoContactItemBinding;
import com.kit.data.kitopeso.net.PU;
import com.kit.data.kitopeso.obj.KitoContactItem;
import com.kit.data.kitopeso.obj.KitoInfoData;
import com.kit.data.kitopeso.tools.LogUtil;
import com.kit.data.kitopeso.tools.PhoneUtils;
import com.kit.data.kitopeso.tools.ResUtils;
import com.kit.data.kitopeso.tools.SpUtils;
import com.kit.data.kitopeso.tools.ViewUtils;
import com.kit.data.kitopeso.view.KitoTextView;

import java.util.List;

public class ContactAdapter extends BaseBindingAdapter1<KitoContactItem, KitoContactItemBinding> {


    String stepTag;

    ClickSelectInterface onClickSelectInterface;

    boolean isError;


    public ContactAdapter(List<KitoContactItem> dataList) {
        super(dataList);
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void bindData(KitoContactItemBinding binding, KitoContactItem item, int position) {


        int p = position + 1;

        if (position <= 0) {
            binding.tvTitle.setText(ResUtils.s(R.string.contact_info_item_title1) + " " + p);
        } else {
            binding.tvTitle.setText(ResUtils.s(R.string.contact_info_item_title1) + " " + p + ResUtils.s(R.string.contact_info_item_title2));
        }


        KitoInfoData rInfoData = item.getRelationData();
        KitoInfoData fInfoData = item.getFullNameData();
        KitoInfoData mInfoData = item.getMobileData();

        if (!TextUtils.isEmpty(rInfoData.getIsMust()) && rInfoData.getIsMust().equals("YES")) {
            binding.tvMust1.setVisibility(ViewGroup.VISIBLE);
        } else {
            binding.tvMust1.setVisibility(ViewGroup.GONE);
        }
        if (!TextUtils.isEmpty(fInfoData.getIsMust()) && fInfoData.getIsMust().equals("YES")) {
            binding.tvMust2.setVisibility(ViewGroup.VISIBLE);
        } else {
            binding.tvMust2.setVisibility(ViewGroup.GONE);
        }
        if (!TextUtils.isEmpty(mInfoData.getIsMust()) && mInfoData.getIsMust().equals("YES")) {
            binding.tvMust3.setVisibility(ViewGroup.VISIBLE);
        } else {
            binding.tvMust3.setVisibility(ViewGroup.GONE);
        }

        binding.tvTitle1.setText(rInfoData.getTitle());
        binding.tvTitle2.setText(fInfoData.getTitle());
        binding.tvTitle3.setText(mInfoData.getTitle());

        binding.tvInput1.setHint(rInfoData.getHint());
        binding.tvInput2.setHint(rInfoData.getHint());
        binding.tvInput3.setHint(fInfoData.getHint());
        binding.tvInput4.setHint(fInfoData.getHint());
        binding.tvInput5.setHint(mInfoData.getHint());
        binding.tvInput6.setHint(mInfoData.getHint());


        binding.tvInput1.setTag(position);
        binding.tvInput3.setTag(position);
        binding.tvInput4.setTag(position);
        binding.tvInput5.setTag(position);
        binding.tvInput6.setTag(position);

        binding.tvInput1.setOnClickListener(null);
        binding.inputLayout1.setOnClickListener(null);
        binding.inputLayout2.setOnClickListener(null);


        boolean isOther = false;
        String otherStr = rInfoData.getParamValueTitle();
        if (!TextUtils.isEmpty(otherStr) && (otherStr.equalsIgnoreCase(ResUtils.s(R.string.other)))) {
            isOther = true;
        }
        boolean isAcc = PhoneUtils.isAccM(KitoApp.getInstance());

        LogUtil.e("me", "isAcc->" + isAcc);

        binding.tvInput1.setType(PU.P28 + stepTag + "_" + rInfoData.getParamName().toUpperCase() + "_CLICK_" + p);
        binding.tvInput2.setType(PU.P28 + stepTag + "_" + rInfoData.getParamName().toUpperCase() + "_INPUT_" + p);
        binding.tvInput3.setType(PU.P28 + stepTag + "_" + fInfoData.getParamName().toUpperCase() + "_INPUT_" + p);
        binding.tvInput4.setType(PU.P28 + stepTag + "_" + fInfoData.getParamName().toUpperCase() + "_CLICK_" + p);
        binding.tvInput5.setType(PU.P28 + stepTag + "_" + mInfoData.getParamName().toUpperCase() + "_INPUT_" + p);
        binding.tvInput6.setType(PU.P28 + stepTag + "_" + mInfoData.getParamName().toUpperCase() + "_CLICK_" + p);

        binding.tvTitle1.setSelected(false);
        binding.tvInput1.setSelected(false);
        binding.tvInput2.setSelected(false);

        binding.tvTitle2.setSelected(false);
        binding.tvInput3.setSelected(false);
        binding.tvInput4.setSelected(false);

        binding.tvTitle3.setSelected(false);
        binding.tvInput5.setSelected(false);
        binding.tvInput6.setSelected(false);


        if (!TextUtils.isEmpty(rInfoData.getParamValueTitle())) {
            binding.tvInput1.setText(rInfoData.getParamValueTitle());
        } else {
            binding.tvInput1.setText("");
            if (!TextUtils.isEmpty(rInfoData.getIsMust()) && rInfoData.getIsMust().equals("YES")) {
                binding.tvTitle1.setSelected(isError);
                binding.tvInput1.setSelected(isError);
                binding.tvInput2.setSelected(false);
            }
        }
        if (!TextUtils.isEmpty(rInfoData.getOther())) {
            binding.tvInput2.setText(rInfoData.getOther());
        } else {
            binding.tvInput2.setText("");
            if (isOther) {
                if (!TextUtils.isEmpty(rInfoData.getIsMust()) && rInfoData.getIsMust().equals("YES")) {
                    binding.tvTitle1.setSelected(isError);
                    binding.tvInput1.setSelected(false);
                    binding.tvInput2.setSelected(isError);
                }
            }
        }


        if (!TextUtils.isEmpty(fInfoData.getParamValueTitle())) {
            binding.tvInput3.setText(fInfoData.getParamValueTitle());
            binding.tvInput4.setText(fInfoData.getParamValueTitle());
        } else {
            binding.tvInput3.setText("");
            binding.tvInput4.setText("");

            if (!TextUtils.isEmpty(fInfoData.getIsMust()) && fInfoData.getIsMust().equals("YES")) {
                binding.tvTitle2.setSelected(isError);
                binding.tvInput3.setSelected(isError);
                binding.tvInput4.setSelected(isError);
            }

        }


        if (!TextUtils.isEmpty(mInfoData.getParamValueTitle())) {
            binding.tvInput5.setText(mInfoData.getParamValueTitle());
            binding.tvInput6.setText(mInfoData.getParamValueTitle());
        } else {
            binding.tvInput5.setText("");
            binding.tvInput6.setText("");
            if (!TextUtils.isEmpty(mInfoData.getIsMust()) && mInfoData.getIsMust().equals("YES")) {
                binding.tvTitle3.setSelected(isError);
                binding.tvInput5.setSelected(isError);
                binding.tvInput6.setSelected(isError);
            }
        }


        if (rInfoData.getAction().equals("CONTACT_GROUP_RELATION")) {
            if (isOther) {
                binding.tvInput2.setVisibility(View.VISIBLE);
                binding.tvInput2.setHint(ResUtils.s(R.string.input_hit));
                binding.tvInput2.setOther(dataList.get(position).getRelationData().getParamValue());
                binding.tvInput2.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        int position = (int) binding.tvInput1.getTag();
                        if (binding.tvInput2.isProgrammaticChange()) {
                            String data = String.valueOf(s);
                            if (!TextUtils.isEmpty(data)) {
                                dataList.get(position).getRelationData().setOther(data);
                                if (isError) {
                                    isError = false;
                                }
                            } else {
                                dataList.get(position).getRelationData().setOther("");
                            }
                        }
                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                    }
                });
            } else {
                binding.tvInput2.setVisibility(View.GONE);
            }
            binding.tvInput1.setOnClickListener(v -> {
                ViewUtils.viewScale(v);
                int index = (int) binding.tvInput1.getTag();
                binding.tvInput1.setClickStartTime(System.currentTimeMillis());
                if (onClickSelectInterface != null) {
                    onClickSelectInterface.onClickItem(2, index, binding.tvInput1, null);
                }
            });
        }

        if (fInfoData.getAction().equals("CONTACT_GROUP_NAME")) {
            if (isAcc) {
                binding.tvInput3.setVisibility(View.VISIBLE);
                binding.tvInput4.setVisibility(View.GONE);
                binding.tvInput3.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        int position = (int) binding.tvInput3.getTag();
                        if (binding.tvInput3.isProgrammaticChange()) {
                            String data = String.valueOf(s);
                            if (!TextUtils.isEmpty(data)) {
                                dataList.get(position).getFullNameData().setParamValue(data);
                                dataList.get(position).getFullNameData().setParamValueTitle(data);
                                if (isError) {
                                    isError = false;
                                }
                            } else {
                                dataList.get(position).getFullNameData().setParamValue("");
                                dataList.get(position).getFullNameData().setParamValueTitle("");
                            }
                        }
                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                    }
                });
            } else {
                binding.tvInput3.setVisibility(View.GONE);
                binding.tvInput4.setVisibility(View.VISIBLE);
                binding.inputLayout1.setOnClickListener(v -> {
                    ViewUtils.viewScale(v);
                    int position2 = (int) binding.tvInput4.getTag();
                    binding.tvInput4.setClickStartTime(System.currentTimeMillis());
                    if (onClickSelectInterface != null) {
                        onClickSelectInterface.onClickItem(3, position2, binding.tvInput4, binding.tvInput6);
                    }
                });
            }
        }

        if (mInfoData.getAction().equals("CONTACT_GROUP_PHONE")) {
            if (isAcc) {
                binding.tvInput5.setVisibility(View.VISIBLE);
                binding.tvInput6.setVisibility(View.GONE);
                binding.tvInput5.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        int position = (int) binding.tvInput5.getTag();
                        if (binding.tvInput5.isProgrammaticChange()) {
                            String data = String.valueOf(s);
                            if (!TextUtils.isEmpty(data)) {
                                dataList.get(position).getMobileData().setParamValue(data);
                                dataList.get(position).getMobileData().setParamValueTitle(data);
                                if (isError) {
                                    isError = false;
                                }
                            } else {
                                dataList.get(position).getMobileData().setParamValue("");
                                dataList.get(position).getMobileData().setParamValueTitle("");
                            }
                        }
                    }

                    @Override
                    public void afterTextChanged(Editable s) {

                    }
                });
            } else {
                binding.tvInput5.setVisibility(View.GONE);
                binding.tvInput6.setVisibility(View.VISIBLE);
                binding.inputLayout2.setOnClickListener(v -> {
                    ViewUtils.viewScale(v);
                    int position1 = (int) binding.tvInput6.getTag();
                    binding.tvInput6.setClickStartTime(System.currentTimeMillis());
                    if (onClickSelectInterface != null) {
                        onClickSelectInterface.onClickItem(3, position1, binding.tvInput4, binding.tvInput6);
                    }
                });
            }
        }


    }

    @Override
    protected int getItemType(KitoContactItem item) {
        return 0;
    }

    @Override
    protected KitoContactItemBinding createBinding(LayoutInflater inflater, ViewGroup parent,
                                                   int viewType) {
        return KitoContactItemBinding.inflate(inflater, parent, false);
    }


    public void sendSelectData(KitoInfoData item, KitoTextView textView) {
        if (item != null && textView != null && !TextUtils.isEmpty(item.getParamValue())) {
            textView.setClickEndTime(System.currentTimeMillis());
            LogUtil.e("me", textView.getClickStartTime() + "<-item.getOther()->" + item.getOther());
            if (!TextUtils.isEmpty(item.getOther())) {
                textView.sendEvent(item.getParamValue() + "$$" + item.getOther());
            } else {
                textView.sendEvent(item.getParamValue());
            }
        }
    }

    public void setOnClickSelectInterface(ClickSelectInterface onClickSelectInterface) {
        this.onClickSelectInterface = onClickSelectInterface;
    }

    public void setStepTag(String stepTag) {
        this.stepTag = stepTag;
    }

    public interface ClickSelectInterface {
        void onClickItem(int type, int position, KitoTextView textView1, KitoTextView textView2);
    }

    public boolean isError() {
        return isError;
    }

    public void setError(boolean error) {
        isError = error;
    }
}
